/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Converters;

import ComponenteEntidad.Tipoingreso;
import ManagedBeans.ManagedBeanTipoIngreso;
import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Noemi
 */
@FacesConverter(value = "TipoIngresoConverter")
public class TipoIngresoConverter implements Converter{
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (context == null) {
            throw new NullPointerException("context");
        }
        if (component == null) {
            throw new NullPointerException("component");
        }
        FacesContext ctx = FacesContext.getCurrentInstance();
        ValueExpression vex = ctx.getApplication().getExpressionFactory().createValueExpression(ctx.getELContext(), "#{managedBeanTipoIngreso}", ManagedBeanTipoIngreso.class);
        ManagedBeanTipoIngreso objetomanagebeans = (ManagedBeanTipoIngreso) vex.getValue(ctx.getELContext());
        Tipoingreso objeto;
        try {
            objeto = objetomanagebeans.getObjetoTipoingreso(new Integer(value));
        } catch (NumberFormatException e) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Valor Desconocido", "Codigo Incorrecto");
            throw new ConverterException(message);
        }
        if (objeto == null) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Valor Desconocido", "El Objeto es Desconocido");
            throw new ConverterException(message);
        }
        return objeto;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (context == null) {
            throw new NullPointerException("context");
        }
        if (component == null) {
            throw new NullPointerException("component");
        }
        if (((Tipoingreso) value).getIdTipoIngreso()!= null) {
            return ((Tipoingreso) value).getIdTipoIngreso().toString();
        } else {
            return null;
        }
    }
}
